#!/bin/sh
set -e

if [ -n "$DB_HOST" ]; then
  echo "Waiting for MySQL at $DB_HOST:${DB_PORT:-3306}..."
  until nc -z "$DB_HOST" "${DB_PORT:-3306}"; do
    sleep 1
  done
fi

if [ -n "$REDIS_HOST" ]; then
  echo "Waiting for Redis at $REDIS_HOST:${REDIS_PORT:-6379}..."
  until nc -z "$REDIS_HOST" "${REDIS_PORT:-6379}"; do
    sleep 1
  done
fi

python manage.py migrate --noinput
python manage.py collectstatic --noinput

exec gunicorn HiiaenOAback.wsgi:application \
  --bind 0.0.0.0:8000 \
  --workers ${GUNICORN_WORKERS:-3} \
  --timeout ${GUNICORN_TIMEOUT:-60}